package aupdg.data.model.impl;

import aupdg.data.model.Node;
import java.util.ArrayList;
import java.util.List;

/**
 * LDG's data dependence edge.
 *
 * @author AA and CJ
 */
public class AuDataDependenceEdge extends AuGenericEdge {

    /**
     * List of data objects related with a data dependence edge. Every time a
     * data dependence is added, the edge is related with a data object. This
     * objects are stored in this list. It is used for explicit information
     * leaks analysis.
     */
    private List<AuLdgData> dataObjects;
    /**
     * List of process instances in the log. Stored in LDG because it used
     * together with dataOjects for explicit information leaks analysis. This
     * analysis is made by instance.
     */
    private List<String> instances;

    /**
     * Default constructor. Invokes the superclass constructor and initializes
     * the lists of data objects and instances.
     *
     * @param source
     * @param target
     * @param label
     */
    public AuDataDependenceEdge(Node source, Node target, String label) {
        super(source, target, label);
        dataObjects = new ArrayList<>();
        instances = new ArrayList<>();
    }

    @Override
    public String getLabel() {
        String edgeLabel = "";
        for (int i = 0; i < this.dataObjects.size(); i++) {
            edgeLabel.concat(this.dataObjects.get(i).getDataLabel());
        }
        return edgeLabel;
    }

    /**
     * Sets the edge's label.
     *
     * @param label
     */
    public void setLabel(String label) {
        this.label = label;
    }

    /**
     * Returns the list of data objects.
     *
     * @return
     */
    public List<AuLdgData> getDataObjects() {
        return this.dataObjects;
    }

    /**
     * Returns list of process instances.
     *
     * @return
     */
    public List<String> getInstances() {
        return this.instances;
    }

    /**
     * Adds a data object to the list of data objects.
     *
     * @param dataObject
     * @param instance
     */
    public void addDataObject(AuLdgData dataObject, String instance) {
        if (!dataObjects.contains(dataObject)) {
            this.dataObjects.add(dataObject);
            this.instances.add(instance);
        }
    }

    @Override
    public String toString() {
        String edgeLabel = "";
        for (int i = 0; i < this.dataObjects.size(); i++) {
            edgeLabel = edgeLabel + "[" + this.dataObjects.get(i).getDataLabel() + "] ";
        }
        return edgeLabel;
    }
}
